package com.yazi.mxz.common.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyEntityNotExistsException;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.exception.MyIllegalArgumentException;
import com.myframework.smhj.common.exception.MyNoPermissionException;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yazi.mxz.common.db.dao.ShoppingCartDao;
import com.yazi.mxz.common.db.dao.ShoppingDao;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.Shopping;
import com.yazi.mxz.common.entity.beans.ShoppingCart;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.entity.custom.ShoppingCart4Order;
import com.yazi.mxz.common.exception.Code;
import com.yazi.mxz.common.exception.MxzStoreGoodsException;
import com.yazi.mxz.common.service.ShoppingCartService;

@Service("shoppingCartService")
public class ShoppingCartImpl extends BasicService<ShoppingCart> implements ShoppingCartService {
	private static Logger log = LoggerFactory.getLogger(ShoppingCartImpl.class);
	
	@Resource
	ShoppingDao shoppingDao;
	@Resource
	ShoppingCartDao shoppingCartDao;

	@Override
	protected Dao<ShoppingCart> getDao() {
		return shoppingCartDao;
	}
	
	@Override
	public List<ShoppingCart> checkForOrderByMember(Member member,
			List<ShoppingCart4Order> cartList) throws MyException  {
		
		log.debug("checkForOrderByMember：memberId={}", member.getMemberId());
		
		// 最终可以下单的购物车及物品
		List<ShoppingCart> destCartList = new ArrayList<ShoppingCart>();
		
		for (ShoppingCart4Order cartForOrder : cartList) {
			
			Long[] shoppingIds = cartForOrder.getShoppingIds();
			Long cartId = cartForOrder.getCartId();
			
			if (null == shoppingIds || shoppingIds.length == 0 || null == cartId) {
				throw new MyIllegalArgumentException();
			}
			
			// 检查购物车
			ShoppingCart existsCart = shoppingCartDao.get(cartId);
			if (null == existsCart) {
				throw new MyEntityNotExistsException("购物车不存在");
			}
			if (!existsCart.getMember().equals(member)) {
				throw new MyNoPermissionException();
			}
			
			// 每个购物车中最终可以下单的物品
			Set<Shopping> destShoppingList = new HashSet<Shopping>();
			
			// 检查物品
			for (Long shoppingId : shoppingIds) {	
				
				Shopping destShopping = shoppingDao.get(shoppingId);
				
				if (null == destShopping) {
					throw new MyEntityNotExistsException("物品不存在");
				}
				if (destShopping.getBuyCount() <= 0) {
					throw new MyIllegalArgumentException();
				}
				if (!destShopping.getShoppingCart().equals(existsCart)) {
					throw new MyNoPermissionException();
				}
				
				// 库存不足
				StoreGoods storeGoods = destShopping.getStoreGoods();
				if (storeGoods.getStockConvert() < destShopping.getBuyCount()) {
					throw new MxzStoreGoodsException(Code.ERR_10002, storeGoods);
				}
				
				destShoppingList.add(destShopping);
			}
			
			ShoppingCart destCart = new ShoppingCart();
			destCart.setCartId(cartForOrder.getCartId());
			destCart.setShoppings(destShoppingList);
			destCart.setStore(existsCart.getStore());
			destCartList.add(destCart);
			
		}
	
		return destCartList;
	}

}
